import request from '@/utils/request';

export type LoginParamsType = {
  LOGIN_NAME: string;
  LOGIN_PASS: string;
};
export type MdopLoginType = {
  userId: string;
  passWord: string;
  code: string;
};

type ProcessParams = {
  empName: string;
  mail: string;
  userStatus: string;
  userid: string;
  token: string;
};

export async function testLogin(params: LoginParamsType) {
  return request('/mdop/login/login', {
    method: 'POST',
    data: params,
  });
}

const urlEncode = (param: any, key: any, encode: any) => {
  if (param == null) return '';
  let paramStr = '';
  const t = typeof param;
  // eslint-disable-next-line eqeqeq
  if (t == 'string' || t == 'number' || t == 'boolean') {
    paramStr += `&${key}=${
      encode == null || encode ? encodeURIComponent(param) : param
    }`;
  } else {
    for (let i in param) {
      let k =
        key == null
          ? i
          : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
      paramStr += urlEncode(param[i], k, encode);
    }
  }
  return paramStr;
};

export async function mdopLogin(params: MdopLoginType) {
  return request('/mdop/login/2/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      Accept:
        'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    },
    data: urlEncode(params).substring(1),
  });
}

export async function mdopLoginOut() {
  return request('/mdop/login/mdop/logOut', {
    method: 'GET',
  });
}

export async function getCheckLoginUrl() {
  return request('/mdop/login/mdop/getCheckLoginUrl', {
    method: 'GET',
  });
}

export async function getProcessCenter(params: ProcessParams) {
  return request(`/ssoService/ssoUser/operationManagement`, {
    method: 'POST',
    data: params,
  });
}

export async function loginDataV(params: any) {
  return request(`/mdop/login/data/getToken`, {
    method: 'POST',
    data: params,
  });
}

export async function queryUrl(params: any) {
  return request(`/mdop/aaa`, {
    method: 'POST',
    data: params,
  });
}
// 验证码校验接口
export async function authCode() {
  return request(`/mdop/login/kaptcha/defaultKaptcha`, {
    method: 'GET',
  });
}
export async function getPublicKey(params: any) {
  return request(`/mdop/login/getPublicKey`, {
    method: 'GET',
  });
}

export async function getVerificationCode(params: any) {
  return request('/mdop/tool/sendMsg/getVerificationCode', {
    method: 'POST',
    data: params,
  });
}

export async function verificationService(params: any) {
  return request('/mdop/login/verificationService', {
    method: 'POST',
    data: params,
  });
}
